package com.aerozhonghuan.archive.task;

import com.aerozhonghuan.archive.service.IncrementalMigrationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class IncrementalMigrationTask {

    private static final Logger logger = LoggerFactory.getLogger(IncrementalMigrationTask.class);

    private final IncrementalMigrationService incrementalMigrationService;

    // 从配置文件中读取批次大小，默认为10000
    @Value("${migration.batchSize:10000}")
    private int batchSize;


    public IncrementalMigrationTask(IncrementalMigrationService incrementalMigrationService) {
        this.incrementalMigrationService = incrementalMigrationService;
    }

    /**
     * 定时执行增量迁移任务
     */
    @Scheduled(cron = "${migration.cron.expression:0 0 0 1 1,4,7,10 ?}")
    public void runIncrementalMigration() {
        try {
            logger.info("定时增量迁移任务启动，批次大小：{}", batchSize);
            String result = incrementalMigrationService.executeIncrementalMigration(batchSize);
            logger.info("定时增量迁移任务执行完成，结果：\n{}", result);
        } catch (Exception e) {
            logger.error("定时增量迁移任务执行异常：", e);
        }
    }
}
